package com.itheima.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.PageVo;
import com.itheima.admin.dto.AddDeptDto;
import com.itheima.admin.dto.DeptPageDto;
import com.itheima.admin.dto.UpdateDeptDto;
import com.itheima.admin.mapper.DepartmentMapper;
import com.itheima.admin.pojo.Department;
import com.itheima.admin.service.IDepartmentService;
import com.itheima.admin.util.IdWorker;
import com.itheima.admin.vo.DeptVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageVo<DeptVo> queryByPage(DeptPageDto deptPageDto) {
        //开启分页
        PageHelper.startPage(deptPageDto.getCurrentPage(), deptPageDto.getPageSize());

        List<Department> list = departmentMapper.selectByPage(deptPageDto.getState(), deptPageDto.getDeptName());
        PageInfo<Department> page = new PageInfo<>(list);

        List<DeptVo> collect = page.getList().stream().map(
                department -> {
                    DeptVo deptVo = new DeptVo();
                    BeanUtils.copyProperties(department, deptVo);
                    deptVo.setStatus(department.getState());
                    Department parentDept = departmentMapper.queryById(department.getParentId());
                    deptVo.setParentName((parentDept == null) ? null : parentDept.getDeptName());
                    return deptVo;
                }
        ).collect(Collectors.toList());

        return new PageVo<>(collect, Long.valueOf(page.getTotal()).intValue());
    }

    @Override
    public List<DeptVo> queryAllDepartments() {


        return departmentMapper.selectList(null).stream().map(
                department -> {
                    DeptVo deptVo = new DeptVo();
                    BeanUtils.copyProperties(department, deptVo);
                    deptVo.setStatus(department.getState());
                    Department parentDept = departmentMapper.queryById(department.getParentId());
                    deptVo.setParentName((parentDept == null) ? null : parentDept.getDeptName());
                    return deptVo;
                }

        ).collect(Collectors.toList());
    }

    @Override
    public boolean addDepartment(AddDeptDto addDeptDto) {
        Department department = new Department();
        BeanUtils.copyProperties(addDeptDto, department);

        //使用UUID生成一个主键
        department.setDeptId(UUID.randomUUID().toString());

        return departmentMapper.insert(department) > 0;
    }

    @Override
    public boolean deleteDepartment(String deptId) {
        return departmentMapper.deleteById(deptId) > 0;
    }

    @Override
    public AddDeptDto queryById(String deptId) {
        Department department = departmentMapper.selectById(deptId);
        AddDeptDto result = new AddDeptDto();
        BeanUtils.copyProperties(department,result);

        return result;
    }

    @Override
    public boolean editDepartment(UpdateDeptDto updateDeptDto) {
        Department department = new Department();
        BeanUtils.copyProperties(updateDeptDto,department);
        return departmentMapper.updateById(department) > 0;
    }
}
